<template>
  <div>
    <el-button-group v-if="showWhole" size="mini">
      <el-button @click="operateAll('COLD')">
        <svg-icon icon-class="snow" /> {{ $t('airCondition.Cold') }}</el-button>
      <el-button icon="el-icon-sunny" @click="operateAll('HOT')">{{ $t('airCondition.Hot') }}</el-button>
      <el-button icon="el-icon-switch-button" @click="operateAll('CLOSE')">{{ $t('Off') }}</el-button>
    </el-button-group>
    <div v-if="showList">
      <div v-for="(item,index) in list" :key="'control_'+index">
        <el-form>
          <el-form-item>
            <el-button-group>
              <el-button @click="onOperate('COLD',item.devMac)">
                <svg-icon icon-class="snow" /> {{ $t('airCondition.Cold') }}</el-button>
              <el-button icon="el-icon-sunny" @click="onOperate('HOT',item.devMac)">{{ $t('airCondition.Hot') }}
              </el-button>
              <el-button icon="el-icon-switch-button" @click="onOperate('CLOSE',item.devMac)">{{ $t('Off') }}
              </el-button>
            </el-button-group>
          </el-form-item>
          <el-form-item :label="$t('Temperature')">
            <el-input-number v-model="temperature" :min="16" :max="31" @change="handleTemp(item.devMac)" />℃
          </el-form-item>
          <el-form-item :label="$t('device.Voltage')">{{ device.message.Voltage }} (V)</el-form-item>
          <el-form-item :label="$t('device.Current')">{{ (device.message.Current/1000).toFixed(3) }} (A)
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
  import Common from './common'
  export default {
    name: "AirCondition",
    mixins: [Common],
    data() {
      return {
        temperature: 17,
        mode: 'CLOSE'
      }
    },
    methods: {
      onOperate(mode, DevMac) {
        this.temperature = 26
        const content = {
          "DeviceControl": {
            DevMac,
            "Message": {
              IrSend: {
                "Mode": mode === 'CLOSE' ? 0 : mode === 'HOT' ? 5 : 2,
                "Order": 255,
                "Temp": this.temperature
              }
            }
          },
          "PackageType": 0
        }
        this.deviceCommand('control', JSON.stringify(content))
      },
      handleTemp(DevMac) {
        var content = {
          "DeviceControl": {
            DevMac,
            "Message": {
              "IrSend": {
                "Mode": this.mode == 'HOT' ? 5 : 2,
                "Order": 255,
                "Temp": this.temperature
              }
            }
          },
          "PackageType": 0
        }
        this.deviceCommand('control', JSON.stringify(content))
      },
      operateAll(mode) {
        for (var i = 0; i < this.list.length; i++) {
          const item = this.list[i]
          this.onOperate(mode, item.devMac)
        }
      },
    },
  }

</script>
